export class DateUtil {
    // 获取当前的星期数.
    public static getFullWeeks(timestamp: number): number {
        // 1970-1-1是周四. 距离下个周日有3天.259200000,时间戳是以08:00开始的,再校正一下,+8时区.
        let weeks = Math.floor((timestamp - 230400000) / 604800000);
        return weeks;
    }

    // 获取当前月数.
    public static getMonths(timestamp: number): number {
        let date = new Date();
        date.setTime(timestamp);
        return date.getUTCMonth()
    }

    // 获取当前年份.
    public static getYears(timestamp: number): number {
        if (!timestamp) {
            timestamp = Date.now();
        }
        let date = new Date();
        date.setTime(timestamp);
        return date.getUTCFullYear()
    }

    // 判断当前星期数是否相等.
    public static isSameWeek(time1: number, time2: number): boolean {
        if (!time1 || !time2) {
            return false;
        }
        return this.getFullWeeks(time1) === this.getFullWeeks(time2)
    }

    // 获取当前周的周一8点钟时间戳.
    public static getToWeekStartTime(): number {
        let now = new Date(); // 当前日期
        let nowDayOfWeek = now.getDay(); // 今天本周的第几天
        let nowDay = now.getDate(); // 当前日
        let nowMonth = now.getMonth(); // 当前月
        let nowYear = now.getFullYear(); // 当前年
        nowYear += (nowYear < 2000) ? 1900 : 0; //
        const date = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
        return date.getTime();
    }

    // 判断当前月数是否相等.
    public static isSameMonth(time1: number, time2: number): boolean {
        if (!time1 || !time2) {
            return false;
        }
        const d1 = new Date();
        const d2 = new Date();
        d1.setTime(time1);
        d2.setTime(time2);
        return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth();
    }

    public static isSameDay(t1: number, t2: number): boolean {
        return Math.floor(t1 / (24 * 3600000)) == Math.floor(t2 / (24 * 3600000));
    }

    // 判断是否是今天.
    public static isToday(ts: number): boolean {
        return Math.floor(ts / (24 * 3600000)) == Math.floor(cc.sys.now() / (24 * 3600000))
    }

    public static timeToString(timeSeconds: number, useHours?: boolean): string {
        timeSeconds = Math.floor(timeSeconds);
        if (timeSeconds <= 0) {
            return useHours ? "00:00:00" : "00:00";
        }

        const seconds = timeSeconds % 60;
        const minutes = useHours ? timeSeconds % 3600 : Math.floor(timeSeconds / 60);
        const hours = useHours ? Math.ceil(timeSeconds / 3600) : 0;
        let str = useHours ? (hours < 10 ? "0" + hours : "" + hours) + ":" : "";
        str += (minutes < 10 ? "0" + minutes : "" + minutes) + ":";
        str += (seconds < 10 ? "0" + seconds : "" + seconds);
        return str;
    }
}